package com.googlecode.payroll

import org.hibernate.*

import static com.googlecode.payroll.util.RoleUtil.*
import static com.googlecode.payroll.util.DateUtil.*

class SalesAmountTotalizerCreateService {

    def sessionFactory

    def create(String month) {

        Date startOfMonth = startOfMonth(month)
        Date endOfMonth = endOfMonth(month)

        String totalAmountQureryStatement = "select sum(salesReceipt.item.price), salesReceipt.employee.id " + 
                                            "from SalesReceipt salesReceipt " + 
                                            "where date >= :startOfMonth and :endOfMonth >= date " +
                                            "group by salesReceipt.employee.id"

        def session = sessionFactory.getCurrentSession()
        Query totalAmountQuery = session.createQuery(totalAmountQureryStatement)
        totalAmountQuery.setParameter("startOfMonth", startOfMonth)
        totalAmountQuery.setParameter("endOfMonth", endOfMonth)

        def records = totalAmountQuery.list()

        assign(records, SalesAmountTotalizer)
    }
}
